/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASWingUtils;
import org.aswing.AttachIcon;
import org.aswing.border.EmptyBorder;
import org.aswing.BorderLayout;
import org.aswing.Box;
import org.aswing.FlowLayout;
import org.aswing.FocusManager;
import org.aswing.geom.Point;
import org.aswing.Icon;
import org.aswing.Insets;
import org.aswing.JButton;
import org.aswing.JFrame;
import org.aswing.JLabel;
import org.aswing.JPanel;
import org.aswing.util.Delegate;
import org.aswing.util.HashMap;

import com.bourre.utils.ClassUtils;

import fever.app.accelerator.Keyboard;
import fever.app.accelerator.ShortcutLocator;
import fever.app.accelerator.ShortcutMap;
import fever.display.ModalScreen;
import fever.events.KeyboardEvent;

import fvaswing.components.alert.FvAlertButton;
import fvaswing.components.alert.FvAlertType;
import fvaswing.components.FvAlert;
import fvaswing.components.FvAlertResources;
import fvaswing.components.FvButton;

/**
 * {@code FvAlertView } class.
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.alert.FvAlertView
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : FvAlertView;
	
	private var _frame : JFrame;
	private var _message : JLabel;
	private var _icon : AttachIcon;
	
	private var _okButton : FvButton;
	private var _yesButton : FvButton;
	private var _noButton : FvButton;
	private var _cancelButton : FvButton;
	private var _abortButton : FvButton;
	private var _ignorelButton : FvButton;
	private var _retryButton : FvButton;
	
	private var _defaultButton : FvAlertButton;
	
	private var _modal : ModalScreen;
	
	private var _keyMap : ShortcutMap;
	private var _buttonMap : HashMap;
	
	private var _bInit : Boolean;
	
	private var _controller : FvAlert;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link FvAlertView} instance.
	 */
	public static function getInstance() : FvAlertView
	{
		if( !_instance ) _instance = new FvAlertView();
		return _instance;	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvAlertView()
	{
		_bInit = false;
		
		_keyMap = ShortcutLocator.getMap( ClassUtils.getFullyQualifiedClassName( this ) );
		
		_keyMap.registerEvent( Keyboard.onKeyENTER, this, _onEnterKey );		_keyMap.registerEvent( Keyboard.onKeyESCAPE, this, _onEscapeKey );
	}
	
	private function _openDialog( 
		controller : FvAlert, 
		type : FvAlertType, 
		title : String, 
		message : String, 
		defaultButton : FvAlertButton, 
		icon : Icon,
		blur : Boolean
		 ) : Void
	{
		_keyMap.load();
		
		if( !_bInit ) _initAlertView();
		
		_controller = controller;
		
		_updateButtonsPanel( type );
		_message.setText( message );
		_frame.setTitle( title );
		
		if( icon ) _message.setIcon( icon );
		
		_layout();
		
		_modal.show();
		_frame.show();
		
		if( _buttonMap.containsKey( defaultButton ) )
		{
			FocusManager.getCurrentManager().setFocusOwner( _buttonMap.get( defaultButton ) );
			JButton( _buttonMap.get( defaultButton ) ).requestFocus();
		} 
		else
		{
			FocusManager.getCurrentManager().setFocusOwner( _buttonMap.get( _defaultButton ) );
			JButton( _buttonMap.get( _defaultButton ) ).requestFocus();
		}
	}
	
	private function _initAlertView() : Void
	{
		_frame = new JFrame( _getModalTarget(), 'Alert', false );
		_frame.setResizable( false );
		_frame.setClosable( false );
		_frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
		_frame.addEventListener( JFrame.ON_WINDOW_CLOSING, Delegate.create( this, _handleClose, FvAlertButton.CANCEL ) );
		
		_frame.getContentPane().setBorder( new EmptyBorder( null, new Insets( 5, 5, 5, 5 ) ) );
		_frame.getContentPane().setLayout( new BorderLayout( 4, 4 ) );
		
		_frame.getContentPane().append ( _createMessagePanel(), BorderLayout.NORTH );
		_frame.getContentPane().append( Box.createVerticalGlue( 25 ), BorderLayout.CENTER );
		_frame.getContentPane().append ( _createButtonPanel(), BorderLayout.SOUTH );
		
		_bInit = true;
	}
	
	private function _getModalTarget( blur : Boolean ) : MovieClip
	{
		_modal.dispose();
		_modal = new ModalScreen();
		_modal.enableBlurEffect = blur;
		
		return _modal.getModalTarget();
	}
	
	private function _createMessagePanel() : JPanel
	{
		_message = new JLabel( '', JLabel.LEFT );
		_message.setIconTextGap( 10 );
		
		var p : JPanel = new JPanel ();
		p.setLayout( new FlowLayout( FlowLayout.CENTER ) );
		p.append ( _message );
		return p;
	}
	
	private function _createButtonPanel() : JPanel
	{
		_buttonMap = new HashMap();
		
		_okButton = _createButton( FvAlertButton.OK, 'okLabel' );
		_yesButton = _createButton( FvAlertButton.YES, 'yesLabel' );
		_noButton = _createButton( FvAlertButton.NO, 'noLabel' );
		_cancelButton = _createButton( FvAlertButton.CANCEL, 'abortLabel' );
		_abortButton = _createButton( FvAlertButton.ABORT, 'cancelLabel' );
		_ignorelButton = _createButton( FvAlertButton.IGNORE, 'ignoreLabel' );
		_retryButton = _createButton( FvAlertButton.RETRY, 'retryLabel' );
		
		var p : JPanel = new JPanel ();
		p.setLayout ( new FlowLayout ( FlowLayout.CENTER, 10, 2 ) );
		p.append( _okButton );
		p.append( _yesButton );
		p.append( _noButton );
		p.append( _abortButton );
		p.append( _retryButton );
		p.append( _ignorelButton );
		p.append( _cancelButton );
		
		return p;
	}
	
	private function _createButton( id : Number, localID ) : FvButton
	{
		var b : FvButton = new FvButton( '' );
		b.addActionListener( Delegate.create( this, _handleClose, id ) );
		b.setPreferredSize( FvAlert.buttonWidth, FvAlert.buttonHeight );
		b.setVisible( false );
		b.setLocalisationID( localID, FvAlertResources.getInstance() );
		
		_buttonMap.put( id, b );
		
		return b;
	}
	
	private function _activeButton( button : FvButton ) : Void
	{
		button.setVisible( true );
		button.setShortcut( null, _keyMap.getName() );
	}
	
	private function _updateButtonsPanel( type : FvAlertType ) : Void
	{
		_clearAllButtons();
		
		switch( type )
		{
			case FvAlertType.OK_ONLY :
			 	_activeButton( _okButton );
			 	_defaultButton = FvAlertButton.OK;
				break;
				
			case FvAlertType.OK_CANCEL :
				_activeButton( _okButton );
				_activeButton( _cancelButton );
				_defaultButton = FvAlertButton.OK;			 
				break;
				
			case FvAlertType.YES_NO :
			 	_activeButton( _yesButton );
				_activeButton( _noButton );
				_defaultButton = FvAlertButton.YES;
				break;
				
			case FvAlertType.YES_NO_CANCEL :
			 	_activeButton( _yesButton );
				_activeButton( _noButton );
				_activeButton( _cancelButton );
				_defaultButton = FvAlertButton.YES;
				break;
				
			case FvAlertType.ABORT_RETRY_IGNORE :
			 	_activeButton( _abortButton );
				_activeButton( _retryButton );
				_activeButton( _ignorelButton );
				_defaultButton = FvAlertButton.ABORT;
				break;
				
			case FvAlertType.RETRY_CANCEL :
			 	_activeButton( _retryButton );
				_activeButton( _cancelButton );
				_defaultButton = FvAlertButton.RETRY;
				break;
		}
	}
	
	private function _clearAllButtons() : Void
	{
		 var a : Array = _buttonMap.values();
		 var len : Number = a.length;
		 
		 for( var i : Number = 0; i < len; i++ )
		 {
		 	var button : FvButton = FvButton( a[i] );
		 	button.setVisible( false );
		 	button.releaseShortcut();
		 }
	}
	
	private function _layout() : Void
	{
		_frame.pack();
		
		var location : Point = ASWingUtils.getScreenCenterPosition();
		location.x -= _frame.getWidth()/2;
		location.y -= _frame.getHeight()/2;
		_frame.setLocation( location );
	}
	
	private function _onEnterKey( event : KeyboardEvent ) : Void
	{
		var o : FvButton = 	FvButton( FocusManager.getCurrentManager().getFocusOwner() );
		if( o ) o.fireActionEvent();
	}
	
	private function _onEscapeKey( event : KeyboardEvent ) : Void
	{
		_handleClose( null, FvAlertButton.CANCEL );
	}
	
	private function _handleClose( button : JButton, id : FvAlertButton ) : Void
	{
		_controller.fireClose( id );
		
		_clearAllButtons();
		_closeDialog();
	}
	
	private function _closeDialog() : Void
	{
		_keyMap.unload();
		
		_modal.hide();
		_frame.tryToClose();	
	}
}